﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.DomainModels
{
    [Serializable]
    public abstract class IdentityBase
    {
        protected Guid Id { get; private set; }

        public Guid GetPersistId()
        {
            return this.Id;
        }

        public Guid New()
        {
            Id = Guid.NewGuid();
            return Id;
        }

        public IdentityBase(Guid id)
        {
            this.Id = id;
        }

        public override string ToString()
        {
            return this.Id.ToString();
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (Object.Equals(obj, null))
            {
                return false;
            }

            if (obj.GetType() != this.GetType())
            {
                return false;
            }

            return this.Id == ((IdentityBase)obj).Id;
        }

        public static bool operator ==(IdentityBase leftValue, IdentityBase rightValue)
        {
            if (Object.Equals(leftValue, null))
            {
                return Object.Equals(rightValue, null);
            }

            return leftValue.Equals(rightValue);
        }

        public static bool operator !=(IdentityBase leftValue, IdentityBase rightValue)
        {
            return !(leftValue == rightValue);
        }
    }
}
